home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Winter Shell 1.0d2 / Source / Libraries / AboutLib / AboutLib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-16  |  2.3 KB  |  118 lines  |  [TEXT/KAHL]

  1. /* Display the application's about dialog.
  2.     93/12/17 aih - version string is read from vers resource
  3.     93/12/16 aih - window's position is saved in prefs
  4.     93/03/12 aih - created */
  5.  
  6. #include <stdio.h>
  7. #include "AboutLib.h"
  8. #include "DialogLib.h"
  9. #include "MemoryLib.h"
  10. #include "MenuLib.h"
  11. #include "PreferencesLib.h"
  12. #include "ResourceConstantsLib.h"
  13. #include "ResourceLib.h"
  14.  
  15. #define iVersionText (1) /* item in dialog for displaying version string */
  16.  
  17. static DialogPtr gAbout;
  18.  
  19. /* get short version string from 'vers' resource */
  20. static void GetVersion(CStr31 str)
  21. {
  22.     Ptr p = NULL;
  23.     short n = 0;
  24.     struct vers {
  25.         char ver1;
  26.         char ver2;
  27.         char level;
  28.         char stage;
  29.         short region;
  30.     };
  31.     
  32.     p = *ResGet('vers', RLVERS_APPLICATION) + sizeof(struct vers);
  33.     n = (*p < sizeof(CStr31) ? *p : sizeof(CStr31) - 1);
  34.     BlockMove(p + 1, str, n);
  35.     str[n] = 0;
  36. }
  37.  
  38. /* open the about dialog */
  39. void AboutOpen(void)
  40. {
  41.     CStr255 fmt, str;
  42.     CStr31 vers;
  43.     volatile DialogPtr about = NULL;
  44.     
  45.     TRY {
  46.         if (gAbout)
  47.             WinSelect(gAbout);
  48.         else {
  49.             about = DlgBegin(RLD_ABOUT);
  50.             DlgText(about, iVersionText, fmt);
  51.             GetVersion(vers);
  52.             sprintf(str, fmt, vers);
  53.             DlgTextSet(about, iVersionText, str);
  54.             DlgCenter(about);
  55.             WinRegister(about, about, AboutEventTable());
  56.             WinZoomRestore(about, PrefsFile(), PREFS_ABOUT_POSITION);
  57.             WinShow(about);
  58.             gAbout = about;
  59.             about = NULL;
  60.         }
  61.     } CATCH {
  62.         DlgEnd(about);
  63.     } ENDTRY;
  64. }
  65.  
  66. /* close the about dialog */
  67. void AboutClose(void)
  68. {
  69.     if (gAbout) {
  70.         WinZoomSave(gAbout, PrefsFile(), PREFS_ABOUT_POSITION);
  71.         WinUnregister(gAbout, gAbout);
  72.         DlgEnd(gAbout);
  73.         gAbout = NULL;
  74.     }
  75. }
  76.  
  77. /* true if about dialog has focus */
  78. static Boolean AboutHasFocus(void)
  79. {
  80.     return(gAbout && WinHasFocus(gAbout));
  81. }
  82.  
  83. /* adjust about menu item */
  84. void AboutAdjustMenu(void)
  85. {
  86.     if (MemWarning() < MEM_WARNING_VERY_LOW) {
  87.         if (! WinModalHasFocus()) MenuCmdEnable(CMD_ABOUT);
  88.         MenuCmdCheck(CMD_ABOUT, AboutHasFocus());
  89.     }
  90.     if (AboutHasFocus())
  91.         MenuCmdEnable(CMD_CLOSE);
  92. }
  93.  
  94. Boolean AboutMenu(const MenuPickType *pick)
  95. {
  96.     Boolean handled = false;
  97.     
  98.     switch (pick->cmd) {
  99.     case CMD_ABOUT:
  100.         AboutOpen();
  101.         handled = true;
  102.         break;
  103.     case CMD_CLOSE:
  104.         if (AboutHasFocus()) {
  105.             AboutClose();
  106.             handled = true;
  107.         }
  108.         break;
  109.     }
  110.     return(handled);
  111. }
  112.  
  113. void AboutMemoryLow(void)
  114. {
  115.     if (MemWarning() >= MEM_WARNING_VERY_LOW)
  116.         AboutClose();
  117. }
  118.